﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web.sysMan
{
    public partial class listKZ : System.Web.UI.Page
    {
        Maticsoft.BLL.studentInfo bll = new Maticsoft.BLL.studentInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //BindData();
            string str = txtKeyword.Text.Trim();
            string pstr = str.Substring(0, str.IndexOf('*'));
            int mnum = str.Length - pstr.Length;            
            int id = 0;
            int j = 0;
            string ks = "";
            string kh = "";
            DataSet ds = new DataSet();
            ds = bll.Getsturoomkz(" order by examRoomName,newid()");
            if (ds.Tables[0].Rows.Count>0)
                ks = ds.Tables[0].Rows[0]["examRoomName"].ToString();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                id = int.Parse(ds.Tables[0].Rows[i]["stuId"].ToString());
                if (ks != ds.Tables[0].Rows[i]["examRoomName"].ToString())
                {
                    ks = ds.Tables[0].Rows[i]["examRoomName"].ToString();                    
                    j = 1;
                }
                else
                {
                    j++;
                }
                kh = pstr + (i+1).ToString().PadLeft(mnum,'0');
                bll.UpdateKZ(id, kh, j);                

            }
            BindData();
        }
       
        #region gridView

        public void BindData()
        {


            DataSet ds = new DataSet();

            ds = bll.Getsturoomkz(" order by stuexamroom");
            gridView.DataSource = ds;
            gridView.DataBind();

        }

       
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                e.Row.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }

        }
        #endregion

        protected void btnOUT_Click(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            string str = dt.ToString("yyyyMMddhhmmss");
            str = str + ".xls";

            //Bind();
            GridViewToExcel(gridView, "application/ms-excel", str);
            //Bind();

        }
        
        public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
        {
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
            HttpContext.Current.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 
            ctrl.Page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctrl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
            /// <summary>
        }
        /// ReLoad this VerifyRenderingInServerForm is neccessary
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control)
        {
        }


       
    }
}